package com.wanmait.recruitment.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.mapper.DistrictMapper;
import com.wanmait.recruitment.pojo.City;
import com.wanmait.recruitment.pojo.Province;
import com.wanmait.recruitment.service.CityService;
import com.wanmait.recruitment.service.ConfigService;
import com.wanmait.recruitment.service.DistrictService;
import com.wanmait.recruitment.service.ProvinceService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.swing.*;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("manage/address")
public class AddressController {
    @Resource
    private ProvinceService provinceService;
    @Resource
    private CityService cityService;
    @Resource
    private DistrictService districtService;
    @Resource
    private ConfigService configService;
    @RequestMapping("list")
    public void list(){

    }
    @RequestMapping("date")
    public ModelAndView date(Province province, String pageNumber){
        ModelAndView modelAndView = new ModelAndView();
        int pageNum;
        try {
            pageNum = Integer.parseInt(pageNumber);
        } catch (NumberFormatException e) {
            pageNum = 1;
        }
        int pageSize;
        try {
            pageSize = Integer.parseInt(configService.getValueByKey("province_config"));
        } catch (NumberFormatException e) {
            pageSize = Integer.parseInt(configService.getDefaultValueByKey("province_config"));
        }
        PageInfo<Province> provincePageInfo = provinceService.findListPage(province,pageNum,pageSize);
        modelAndView.addObject("provincePageInfo",provincePageInfo);
        return modelAndView;
    }
    @RequestMapping("updataProvinceName")
    @ResponseBody
    public void updataProvinceName(Province province){
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        province.setDateUpdated(timestamp);
        provinceService.update(province);
    }
    @RequestMapping("delProvince")
    @ResponseBody
    public void delProvince(String id){
        System.out.println(id);
        provinceService.delete(Integer.parseInt(id));
    }
    @RequestMapping("addProvince")
    @ResponseBody
    public void addProvince(Province province){
        provinceService.insert(province);
    }
    @RequestMapping("city")
    public ModelAndView city(String id){
        ModelAndView modelAndView = new ModelAndView();
        List<City> cities = null;
        try {
            cities = cityService.selectByProvinceId(Integer.parseInt(id));
        } catch (Exception e) {
           modelAndView.setViewName("manage/address/list");
        }
        modelAndView.addObject("citys",cities);
        return modelAndView;
    }
    @RequestMapping("updateTop")
    @ResponseBody
    public void updateHop(String id){
        cityService.updateHop(Integer.parseInt(id));
    }
    @RequestMapping("updateHot")
    @ResponseBody
    public void updateHot(String id){
        cityService.updateHot(Integer.parseInt(id));
    }
    @RequestMapping("updataCityName")
    @ResponseBody
    public void updataCityName(City city){
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        city.setDateUpdated(timestamp);
        cityService.updataCityName(city);
    }
    @RequestMapping("delCity")
    @ResponseBody
    public void delCity(String id){
        cityService.delete(Integer.parseInt(id));
    }

}
